//
//  MovieTableViewCell.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/22.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "MovieTableViewCell.h"
#import "NetTool.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#import "SmileModel.h"
#import "UIImageView+AFNetWorking.h"
#import "LHMGetHeightTool.h"

@interface MovieTableViewCell ()

@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) AVPlayer *myPlayer;
@property (nonatomic, retain) AVPlayerItem *item;
@property (nonatomic, retain) AVPlayerLayer *playerLayer;
@property (nonatomic, retain) UIButton *playButton;
@property (nonatomic, retain) UIImageView *playImageView;
@property (nonatomic ,retain) UIButton *zanting;

@end

@implementation MovieTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_titleLabel];
        
        self.playImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_playImageView];
        
        self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
    }
    return self;
}

-(void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat titleHeight = [LHMGetHeightTool heightWith:self.smileModel.text];
    self.titleLabel.frame = CGRectMake(10, 35, self.contentView.bounds.size.width - 20, titleHeight);
    
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.backgroundColor = [UIColor whiteColor];
    
    
    self.playImageView.frame = CGRectMake(10, titleHeight + 35, self.contentView.bounds.size.width - 20, 300);
    
    self.playButton.frame = CGRectMake(self.contentView.bounds.size.width / 2 - 40, 160, 100, 100);
    [self.playButton setImage:[UIImage imageNamed:@"播放 (1)"] forState:UIControlStateNormal];
    [self.playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.playerLayer.frame = CGRectMake(10, titleHeight + 35, self.contentView.bounds.size.width - 20, 300);
    [self addSubview:_playButton];
    
    NSLog(@"%@", self.smileModel);
    
}

-(void)playAction:(UIButton *)button {
    
    
    self.playButton.hidden = YES;
    self.playImageView.image = [UIImage imageNamed:@""];
    self.playImageView.backgroundColor = [UIColor blackColor];
    NSArray *videoArray = [self.smileModel.video objectForKey:@"video"];
    
    NSString *url = [videoArray objectAtIndex:0];
    
    NSURL *mediaURL = [NSURL URLWithString:url];
    self.item = [AVPlayerItem playerItemWithURL:mediaURL];
    self.myPlayer = [AVPlayer playerWithPlayerItem:_item];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:_myPlayer];
    
    [self.layer addSublayer:self.playerLayer];
    
    //    [self.item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    
    //    if (self.isReadToPlay) {
    //        [self.myPlayer play];
    //    }
    [self.myPlayer play];
    
    UIButton *zanting = [UIButton buttonWithType:UIButtonTypeCustom];
    zanting.frame = CGRectMake(self.contentView.bounds.size.width / 2 - 40, 160, 100, 100);
    zanting.backgroundColor = [UIColor clearColor];
    [zanting addTarget:self action:@selector(zanting:) forControlEvents:UIControlEventTouchUpInside];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerPlayToEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    
}

-(void)zanting:(UIButton *)button {
    
    [self.myPlayer pause];
    
    
}

-(void)playerPlayToEnd:(NSNotification *)notification {
    
    NSArray *thumbnailArray = [self.smileModel.video objectForKey:@"thumbnail"];
    NSString *str = [thumbnailArray objectAtIndex:0];
    NSURL *url = [NSURL URLWithString:str];
    [self.playImageView setImageWithURL:url];
    self.playButton.hidden = NO;
    self.playImageView.backgroundColor = [UIColor whiteColor];
    [self.playerLayer removeFromSuperlayer];
    
    
}

-(void)setSmileModel:(SmileModel *)smileModel {
    
    if (_smileModel != smileModel) {
        
        _smileModel = smileModel;
        
    }
    
    self.titleLabel.text = smileModel.text;
    
    NSArray *thumbnailArray = [self.smileModel.video objectForKey:@"thumbnail"];
    
    NSString *str = [thumbnailArray objectAtIndex:0];
    
    NSURL *url = [NSURL URLWithString:str];
    
    [self.playImageView setImageWithURL:url];
    
}

-(void)setIsSelect:(BOOL)isSelect {
    
    _isSelect = isSelect;
    
    if (isSelect == YES) {
        [self.myPlayer pause];
        [self.playerLayer removeFromSuperlayer];
        self.isSelect = NO;
    }
    
}

@end
